// BaseConfig.cpp : implementation file
//

#include "stdafx.h"
//#include "Globe.h"

#include "BaseConfig.h"
// #include "DirectCfg.h"
// #include "Language.h"
// #include "export.h"
// #include "ConfigWordSheet.h"
#include <math.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
WORD nConfigWord[32];
void makeRect(int X,int Y,int W,int H,CRect& r)
{
	r.top = Y;
	r.left = X;
	r.bottom = Y + H;
	r.right = X + W;
}
/////////////////////////////////////////////////////////////////////////////
// CBaseConfig property page

IMPLEMENT_DYNCREATE(CBaseConfig, CPropertyPage)

CBaseConfig::CBaseConfig() : CPropertyPage(CBaseConfig::IDD)
{
	//{{AFX_DATA_INIT(CBaseConfig)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	bInitPage=FALSE;
	bFirstInit=TRUE;
}

CBaseConfig::~CBaseConfig()
{
	int i,j;

	if(pMultiBit)
	{	
		for(j=0;j<nNumofMultiBit;j++)
		{
			for(i=0;i<pMultiBit[j].nNumofSmall;i++)
			{
				if(pMultiBit[j].pSmall[i].pRadio)
					delete pMultiBit[j].pSmall[i].pRadio;
			}
			delete pMultiBit[j].pSmall;
			if(pMultiBit[j].pGroupBox)
				delete pMultiBit[j].pGroupBox;
			if(pMultiBit[j].pComboBox)
				delete pMultiBit[j].pComboBox;
			if(pMultiBit[j].pStatic)
				delete pMultiBit[j].pStatic;
		}
		delete pMultiBit;
	}

	if(pBit)
	{
		for(j=0;j<nNumofBit;j++)
		{
			if(pBit->pSubBit[j].pCheck)
				delete pBit->pSubBit[j].pCheck;
		}
		delete pBit->pSubBit;
		if(pBit->pGroupBox)
			delete pBit->pGroupBox;
		delete pBit;
	}
	if(pWord)
	{
		for(j=0;j<nNumofWord;j++)
		{
			if(pWord[j].pEdit)
				delete pWord[j].pEdit;
			if(pWord[j].pStatic)
				delete pWord[j].pStatic;
		}
		delete pWord;
	}
	if(pByte)
	{
		for(j=0;j<nNumofByte;j++)
		{
			if(pByte[j].pEdit)
				delete pByte[j].pEdit;
			if(pByte[j].pStatic)
				delete pByte[j].pStatic;
		}
		delete pByte;
	}
}

void CBaseConfig::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CBaseConfig)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CBaseConfig, CPropertyPage)
	//{{AFX_MSG_MAP(CBaseConfig)
	//}}AFX_MSG_MAP
	ON_WM_SHOWWINDOW()
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CBaseConfig message handlers

void CBaseConfig::MyGetRect(int nID, LPRECT r)
{
	CWnd *pWnd=GetDlgItem(nID);
	pWnd->GetWindowRect(r);
	ScreenToClient(r);
}

BOOL CBaseConfig::OnCommand(WPARAM wParam, LPARAM lParam) 
{
	// TODO: Add your specialized code here and/or call the base class
		int i,j,k;

	i=wParam;
	if(i>=400 && i<430)
	{
		if(nNumofMultiBit)
		{
			for(j=0;j<nNumofMultiBit;j++)
			{
				if((i>=pMultiBit[j].nFirstID) && (i<pMultiBit[j].nFirstID+pMultiBit[j].nNumofSmall))
				{
					for(k=0;k<pMultiBit[j].nNumofSmall;k++)
					{
						BOOL bStatus=FALSE;
						if(pMultiBit[j].pSmall[k].nID == i)
						{
							pMultiBit[j].nWhichSelect=k;
							bStatus=TRUE;
						}
						(pMultiBit[j].pSmall[k].pRadio)->SetCheck(bStatus);	
					}
				}
			}
		}
	}
	return CPropertyPage::OnCommand(wParam, lParam);
}

BOOL CBaseConfig::OnInitDialog() 
{
	CPropertyPage::OnInitDialog();
#if 1
	// TODO: Add extra initialization here
	

	DlgSetConfigWord(this);

	CWnd* pWnd=GetDlgItem(IDC_EDIT1);
	//CWnd* pWnd ;
	DWORD dwEDITEXTYPE = pWnd->GetExStyle();
	DWORD BoxStyle = pWnd->GetStyle() | WS_VISIBLE;

	DWORD StatStyle = WS_CHILD | WS_VISIBLE ;
	DWORD GroupStyle= WS_CHILD | WS_VISIBLE | BS_GROUPBOX | WS_TABSTOP;
	DWORD CheckStyle= WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX | WS_TABSTOP;
	DWORD RadioStyle= WS_CHILD | WS_VISIBLE | BS_RADIOBUTTON | BS_NOTIFY | WS_TABSTOP;
	DWORD ComBoxStyle= WS_CHILD | WS_VISIBLE | WS_VSCROLL| CBS_DROPDOWNLIST | WS_TABSTOP;
#if 1
	CRect r;
	MyGetRect(IDC_STATIC1,&r);
	SIZEOFSTATICX=r.right-r.left;
	POSEOFSTATICXLEFT=r.left;
	POSEOFSTATICY=r.top;

	MyGetRect(IDC_STATIC2,&r);
	POSEOFSTATICXRIGHT=r.left; 
	
	MyGetRect(IDC_EDIT1,&r);
	SIZEOFEDITX=r.right-r.left;
	POSEOFEDITXLEFT=r.left; 
	POSEOFEDITY=r.top;

	MyGetRect(IDC_EDIT2,&r);
	POSEOFEDITXRIGHT=r.left; 

	MyGetRect(IDC_GROUP1,&r);
	GROUPWIDTH1=r.right-r.left;	

	MyGetRect(IDC_GROUP3,&r);
	GROUPWIDTH2=r.right-r.left;
	GROUPXRIGHT=r.left;

	MyGetRect(IDC_CHECK1,&r);
	BUTTONLEFTX=r.left;
	SIZEBUTTONX=r.right-r.left;

	MyGetRect(IDC_CHECK2,&r);
	BUTTONRIGHTX=r.left;

	MyGetRect(IDC_STATIC_DROP,&r);
	SIZEOFCCOMBOX_STATICX=r.right-r.left;
	MyGetRect(IDC_COMBO_DROP,&r);
	SIZEOFCCOMBOX_X=r.right-r.left;
#endif
	CFont* pFont=GetFont();
	CDC* pDC=GetDC();

	BOOL bLeft;
	int i,j,k;
	int nCurX,nCurY;
	int nNextY,nNextY1;
//	char chTmp[10];
//	int nTmp;

	TEXTMETRIC tm;
	pDC->GetTextMetrics(&tm);
	SIZEOFSTATICY=tm.tmHeight;
	SIZEOFCCOMBOX_Y=tm.tmHeight+2;
	SIZEOFEDITY=tm.tmHeight+2;
	SIZEBUTTONY=tm.tmHeight;


	i=0;j=0;
	bLeft=TRUE;
	if(nNumofByte)
	{
		for(k=0;k<nNumofByte;k++)
		{
			if(bLeft)
				nCurX=POSEOFSTATICXLEFT;
			else
				nCurX=POSEOFSTATICXRIGHT;
			nCurY=POSEOFSTATICY+i*(SIZEOFEDITY+INTERNALINTWO);
			makeRect(nCurX,nCurY,SIZEOFSTATICX,SIZEOFSTATICY,r);
			pByte[k].pStatic=new CStatic();
			pByte[k].pStatic->Create(pByte[k].chName,StatStyle,r,this);
			pByte[k].pStatic->SetFont(pFont);

			if(bLeft)
				nCurX=POSEOFEDITXLEFT;
			else
				nCurX=POSEOFEDITXRIGHT;
			nCurY=POSEOFEDITY+i*(SIZEOFEDITY+INTERNALINTWO);
			makeRect(nCurX,nCurY,SIZEOFEDITX,SIZEOFEDITY,r);
			pByte[k].pEdit=new CEdit();
			((CWnd* )pByte[k].pEdit)->CreateEx(dwEDITEXTYPE, _T("EDIT"), NULL,
				BoxStyle,
				r.left, r.top,
				r.right - r.left, r.bottom - r.top,
				this->GetSafeHwnd(), (HMENU)(BYTEID+k), (LPVOID)NULL);

			pByte[k].pEdit->SetFont(pFont);
			pByte[k].pEdit->LimitText(2);

			if(!bLeft)
				i++;
			bLeft=!bLeft;	
		}
		nNextY=POSEOFSTATICY+(nNumofByte/2+1)*(SIZEOFEDITY+INTERNALINTWO);
		nNextY1=POSEOFEDITY+(nNumofByte/2+1)*(SIZEOFEDITY+INTERNALINTWO);
	}
	else
	{
		nNextY=POSEOFSTATICY;
		nNextY1=POSEOFEDITY;
	}

	i=0;
	bLeft=TRUE;
	if(nNumofWord)
	{
		for(k=0;k<nNumofWord;k++)
		{
			if(bLeft)
				nCurX=POSEOFSTATICXLEFT;
			else
				nCurX=POSEOFSTATICXRIGHT;
			nCurY=nNextY+i*(SIZEOFEDITY+INTERNALINTWO);
			makeRect(nCurX,nCurY,SIZEOFSTATICX,SIZEOFSTATICY,r);
			pWord[k].pStatic=new CStatic();
			pWord[k].pStatic->Create(pWord[k].chName,StatStyle,r,this);
			pWord[k].pStatic->SetFont(pFont);

			if(bLeft)
				nCurX=POSEOFEDITXLEFT;
			else
				nCurX=POSEOFEDITXRIGHT;

			nCurY=nNextY1+i*(SIZEOFEDITY+INTERNALINTWO);
			makeRect(nCurX,nCurY,SIZEOFEDITX,SIZEOFEDITY,r);
			pWord[k].pEdit=new CEdit();
			((CWnd* )pWord[k].pEdit)->CreateEx(dwEDITEXTYPE, _T("EDIT"), NULL,
				BoxStyle,
				r.left, r.top,
				r.right - r.left, r.bottom - r.top,
				this->GetSafeHwnd(), (HMENU)(WORDID+k), (LPVOID)NULL);

			pWord[k].pEdit->SetFont(pFont);
			//Modified by QRJ,2012.03.01
			if(pWord[k].nDecimal==-1){
				pWord[k].pEdit->LimitText(4);
			}
//			nTmp=nConfigWord[pWord[k].nPos] & 0xffff;
//			sprintf(chTmp,"%04X",nTmp);
//			pWord[k].pEdit->SetWindowText(chTmp);

			if(!bLeft)
				i++;
			bLeft=!bLeft;	
		}
		nNextY=nNextY+(nNumofWord/2+1)*(SIZEOFEDITY+INTERNALINTWO);
	}

//	BOOL bStatus;
	i=0;
	nNextY+=INTERNALINTWO;
	bLeft=TRUE;
	if(nNumofBit)
	{
		nCurX=POSEOFSTATICXLEFT;
		nCurY=nNextY;
		makeRect(nCurX,nCurY,GROUPWIDTH1,(SIZEOFEDITY+INTERNALINTWO)*(1+nNumofBit/2+nNumofBit%2),r);
		pBit->pGroupBox=new CButton();
		pBit->pGroupBox->Create(pBit->chName,GroupStyle,r,this,0xffff);
		pBit->pGroupBox->SetFont(pFont);

		for(k=0;k<nNumofBit;k++)
		{
			if(bLeft)
				nCurX=BUTTONLEFTX;
			else
				nCurX=BUTTONRIGHTX;
			nCurY=nNextY+(i+1)*(SIZEOFEDITY+INTERNALINTWO);
			makeRect(nCurX,nCurY,SIZEBUTTONX,SIZEBUTTONY,r);
			pBit->pSubBit[k].pCheck=new CButton();
			(pBit->pSubBit[k].pCheck)->Create(pBit->pSubBit[k].chName,CheckStyle,r,this,BITID+k);
			(pBit->pSubBit[k].pCheck)->SetFont(pFont);
//			bStatus=FALSE;
//			if(pBit->pSubBit[k].bEnable)
//			{
//				if(pBit->pSubBit[k].nEnable & nConfigWord[pBit->pSubBit[k].nPos])
//					bStatus=TRUE;
//			}
//			else
//			{
//				if(pBit->pSubBit[k].nDisable & nConfigWord[pBit->pSubBit[k].nPos])
//					bStatus=FALSE;
//				else
//					bStatus=TRUE;
//			}
//			(pBit->pSubBit[k].pCheck)->SetCheck(bStatus);

			if(!bLeft)
				i++;
			bLeft=!bLeft;	
		}
		nNextY=nNextY+(nNumofBit/2+1+1)*(SIZEOFEDITY+INTERNALINTWO);
	}

	i=0;
	nNextY+=INTERNALINTWO+20;
	bLeft=TRUE;
	int CurLeftY=nNextY,CurRightY=nNextY;
	int nTmpY;
	if(nNumofMultiBit)
	{
		for(j=0;j<nNumofMultiBit;j++)
		{
			if(nConfigType == 10)
			{
				nCurX=POSEOFSTATICXLEFT;
				nCurY=CurLeftY;

				makeRect(nCurX,nCurY,SIZEOFCCOMBOX_STATICX,SIZEOFSTATICY,r);
				pMultiBit[j].pStatic=new CStatic();
				pMultiBit[j].pStatic->Create(pMultiBit[j].chName,StatStyle,r,this);
				pMultiBit[j].pStatic->SetFont(pFont);

				CurLeftY += SIZEOFSTATICY-INTERNALINTWO;
				nCurY=CurLeftY;
				makeRect(nCurX,nCurY,SIZEOFCCOMBOX_X,SIZEOFCCOMBOX_Y*(DROPLISTHEIGHT+1),r);
				pMultiBit[j].pComboBox=new CComboBox();
				pMultiBit[j].pComboBox->Create(ComBoxStyle,r,this,pMultiBit[j].nFirstID);
				pMultiBit[j].pComboBox->SetFont(pFont);
				for(k=0;k<pMultiBit[j].nNumofSmall;k++)
					pMultiBit[j].pComboBox->AddString(pMultiBit[j].pSmall[k].chName);
				CurLeftY += SIZEOFCCOMBOX_Y+ 4*INTERNALINTWO;
			}
			else  ///if(nConfigType == 0)
			{
				if(bLeft)
				{
					nCurX=POSEOFSTATICXLEFT;
					nCurY=CurLeftY;
				}
				else
				{
					nCurX=GROUPXRIGHT;
					nCurY=CurRightY;
				}
				makeRect(nCurX,nCurY,GROUPWIDTH2,(SIZEOFEDITY+INTERNALINTWO)*(1+pMultiBit[j].nNumofSmall),r);
				pMultiBit[j].pGroupBox=new CButton();
				pMultiBit[j].pGroupBox->Create(pMultiBit[j].chName,GroupStyle,r,this,0xffff);
				pMultiBit[j].pGroupBox->SetFont(pFont);

				nTmpY=nCurY+(SIZEOFEDITY+INTERNALINTWO);
//				BOOL bOK=FALSE;
				for(k=0;k<pMultiBit[j].nNumofSmall;k++)
				{
					if(bLeft)
						nCurX=BUTTONLEFTX;
					else
						nCurX=BUTTONRIGHTX;
					nCurY=nTmpY+k*(SIZEOFEDITY+INTERNALINTWO);
					makeRect(nCurX,nCurY,SIZEBUTTONX,SIZEBUTTONY,r);
					pMultiBit[j].pSmall[k].pRadio=new CButton();
					(pMultiBit[j].pSmall[k].pRadio)->Create(pMultiBit[j].pSmall[k].chName,RadioStyle,r,this,pMultiBit[j].nFirstID+k);
					(pMultiBit[j].pSmall[k].pRadio)->SetFont(pFont);
//					bStatus=FALSE;
//					if(((~(pMultiBit[j].nProgram) & 0xffff) & nConfigWord[pMultiBit[j].nPos])==pMultiBit[j].pSmall[k].nValue)
//					{
//						pMultiBit[j].nWhichSelect=k;
//						bStatus=TRUE;
//						bOK=TRUE;
//					}
//					(pMultiBit[j].pSmall[k].pRadio)->SetCheck(bStatus);	
				}
//				if(!bOK)
//				{
//					pMultiBit[j].nWhichSelect=0;
//				}

				if(bLeft)
					CurLeftY+=(pMultiBit[j].nNumofSmall+2)*(SIZEOFEDITY+INTERNALINTWO)+INTERNALINTWO;
				else
					CurRightY+=(pMultiBit[j].nNumofSmall+2)*(SIZEOFEDITY+INTERNALINTWO)+INTERNALINTWO;

				bLeft=!bLeft;
			}
		}
	}
	
	SetAllSelect();

	bInitPage=TRUE;
	bFirstInit=FALSE;

	ReleaseDC(pDC);
	CRect rect;
	GetWindowRect(rect);
	SetWindowPos(NULL,0,0,rect.Width(),0,SWP_NOMOVE|SWP_NOZORDER);
	m_cy=CurLeftY+INTERNALINTWO+20;
#endif
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CBaseConfig::SetAllSelect()
{
/*	PRJ& prj=glPrjList[sysValue.curModule];
	WORD* nConfigWord=(WORD*)prj.par.configWord;*/
	int i,j,k;
	TCHAR chTmp[10];
	int nTmp;
	if(nNumofByte)
	{
		for(k=0;k<nNumofByte;k++)
		{
			if(pByte[k].bLow8Bit)
				nTmp=nConfigWord[pByte[k].nPos] & 0xff;
			else
				nTmp=(nConfigWord[pByte[k].nPos]>>8)& 0xff;
			_stprintf(chTmp,_T("%02X"),nTmp);
			pByte[k].pEdit->SetWindowText(chTmp);
		}
	}

	i=0;
	if(nNumofWord)
	{
		for(k=0;k<nNumofWord;k++)
		{
			nTmp=nConfigWord[pWord[k].nPos] & 0xffff;
			//Modified by QRJ, 2012.03.01
			if(pWord[k].nDecimal==-1){
				_stprintf(chTmp,_T("%04X"),nTmp);
			}else{
				CString fmt;
				fmt.Format(_T("%%.%df"),pWord[k].nDecimal);
				_stprintf(chTmp,fmt,nTmp/pow((double)10,(double)pWord[k].nDecimal));
			}
			pWord[k].pEdit->SetWindowText(chTmp);
		}
	}

	BOOL bStatus;
	i=0;
	if(nNumofBit)
	{
		for(k=0;k<nNumofBit;k++)
		{
			bStatus=FALSE;
			if(pBit->pSubBit[k].bEnable)
			{
				if(pBit->pSubBit[k].nEnable & nConfigWord[pBit->pSubBit[k].nPos])
					bStatus=TRUE;
			}
			else
			{
				if(pBit->pSubBit[k].nDisable & nConfigWord[pBit->pSubBit[k].nPos])
					bStatus=FALSE;
				else
					bStatus=TRUE;
			}
			(pBit->pSubBit[k].pCheck)->SetCheck(bStatus);

		}
	}

	i=0;
	if(nNumofMultiBit)
	{
		for(j=0;j<nNumofMultiBit;j++)
		{
			if(nConfigType == 10)
			{
				pMultiBit[j].nWhichSelect=-1;
				for(k=0;k<pMultiBit[j].nNumofSmall;k++)
				{
					if(((~(pMultiBit[j].nProgram) & 0xffff) & nConfigWord[pMultiBit[j].nPos])==pMultiBit[j].pSmall[k].nValue)
						pMultiBit[j].nWhichSelect=k;
				}
				if(pMultiBit[j].nWhichSelect < 0)
				{
					if(bFirstInit)
					{
						pMultiBit[j].nWhichSelect=pMultiBit[j].pComboBox->AddString(_T("No Select"));
					}
					else
						pMultiBit[j].nWhichSelect=pMultiBit[j].nNumofSmall;
				}
				else
				{
					int nCount=pMultiBit[j].pComboBox->GetCount();
					if(nCount > pMultiBit[j].nNumofSmall)
					{
						pMultiBit[j].pComboBox->DeleteString(nCount-1);
					}
				}

				pMultiBit[j].pComboBox->SetCurSel(pMultiBit[j].nWhichSelect);
			}
			else //if(nConfigType == 0)
			{
				pMultiBit[j].nWhichSelect=0;
				for(k=0;k<pMultiBit[j].nNumofSmall;k++)
				{
					bStatus=FALSE;
					if(((~(pMultiBit[j].nProgram) & 0xffff) & nConfigWord[pMultiBit[j].nPos])==pMultiBit[j].pSmall[k].nValue)
					{
						pMultiBit[j].nWhichSelect=k;
						bStatus=TRUE;
					}
					(pMultiBit[j].pSmall[k].pRadio)->SetCheck(bStatus);	
				}
			}
		}
	}
}
void CBaseConfig::OnOK() 
{
	// TODO: Add extra validation here
/*	PRJ& prj=glPrjList[sysValue.curModule];
	WORD* nConfigWord=(WORD*)prj.par.configWord;*/
	
	Apply();
	CPropertyPage::OnOK();
	
}

BOOL DlgSetConfigWord(CWnd* pParent)
{
	
	//DEVINFO& devInfo=glDevInfoList[sysValue.curModule];
	//if(devInfo.devInfo_3.configWord==_T(""))
	//	return FALSE;
	
	TCHAR chConfigFile[MAX_PATH];
//	_stprintf(chConfigFile,CONFIGFILEPATH,devInfo.devInfo_3.configWord);

#ifndef AGSOFT
	//DecryptFile(chConfigFile,TMPFILE);
	//_tcscpy(chConfigFile,TMPFILE);
#endif


	TCHAR txt[0x100];
	//GetLanguage("/Language/ConfigWordDlg/Title",txt);
	BOOL bInWizard=FALSE;
	if(pParent!=NULL)
		bInWizard=TRUE;
//	CConfigWordSheet dlgPropertySheet(txt);
	
	BOOL bRnt=FALSE;	
	int i,j,k;
	int nTmp1;
	TCHAR* chTmp;
	int nConfigType;
	int nNumofDirect;
	int nShowType;
	
	
	wcscpy_s(chConfigFile, L"c:\\SYEMMC4_2.con");
	
	
	
	nConfigType=GetPrivateProfileInt(_T("Number"),_T("Type"),0,chConfigFile);

	int nTotalPage = GetPrivateProfileInt(_T("Number"),_T("nTotalPage"),10,chConfigFile);
	if(nTotalPage>8 && nTotalPage<1)
	{
//		AfxMessageBox(IDS_ERRORCONFIGFILE);
		return FALSE;
	}

	int* pnNumofByte = new int[nTotalPage];
	int* pnNumofWord = new int[nTotalPage];
	int* pnNumofBit = new int[nTotalPage];
	int* pnNumofMultiBit = new int[nTotalPage];

	int nNumofByte;
	int nNumofWord;
	int nNumofBit;
	int nNumofMultiBit;

	ConfigByte* pByte;
	ConfigWord* pWord;
	ConfigBit* pBit;
	ConfigMultiBit* pMultiBit;
	ConfigDirect* pDirect;
	CBaseConfig* baseconfig=new CBaseConfig[nTotalPage]();

	chTmp=new TCHAR[6*nTotalPage];
	for(i=0;i<nTotalPage;i++)
	{
		pnNumofByte[i]=0;
		pnNumofWord[i]=0;
		pnNumofBit[i]=0;
		pnNumofMultiBit[i]=0;

		_stprintf(&chTmp[6*i],_T("Page%i"),i);
		baseconfig[i].m_psp.pszTitle=&chTmp[6*i];
		baseconfig[i].m_psp.dwFlags |= PSP_USETITLE;
	}

	TCHAR chTmp1[20];
	nNumofDirect = GetPrivateProfileInt(_T("Number"),_T("DirectNum"),0,chConfigFile);
	if(nNumofDirect>32 && nNumofDirect<0)
	{
	//	AfxMessageBox(IDS_ERRORCONFIGFILE);
		return FALSE;
	}
	if(nNumofDirect)
	{
		nShowType=GetPrivateProfileInt(_T("Number"),_T("ShowType"),0,chConfigFile); 
		pDirect=new ConfigDirect[nNumofDirect];

		for(i=0;i<nNumofDirect;i++)
		{
			_stprintf(chTmp1,_T("Direct%i"),i);	

			pDirect[i].pEdit=NULL;
			pDirect[i].pStatic=NULL;

			pDirect[i].nID=DIRECTID+i;
			GetPrivateProfileString(chTmp1,_T("Name"),_T(""),pDirect[i].chName,MAXDIRECTNAME,chConfigFile);
			nTmp1=GetPrivateProfileInt(chTmp1,_T("Pos"),10,chConfigFile);
			pDirect[i].nPos=nTmp1;
			nTmp1=GetPrivateProfileInt(chTmp1,_T("Byte"),10,chConfigFile);
			pDirect[i].nByteType=nTmp1;
			nTmp1=GetPrivateProfileInt(chTmp1,_T("Pos8or16Bit"),10,chConfigFile);
			pDirect[i].nTypeInPos=nTmp1;
		}
	}

	nNumofByte = GetPrivateProfileInt(_T("Number"),_T("Byte"),0,chConfigFile);
	if(nNumofByte>63 && nNumofByte<0)
	{
//		AfxMessageBox(IDS_ERRORCONFIGFILE);
		return FALSE;
	}
	
	for(i=0;i<nNumofByte;i++)
	{
		_stprintf(chTmp1,_T("Byte%i"),i);
		nTmp1 = GetPrivateProfileInt(chTmp1,_T("Page"),10,chConfigFile);
		if(nTmp1>nTotalPage && nTmp1<1)
		{
//			AfxMessageBox(IDS_ERRORCONFIGFILE);
			return FALSE;
		}
		pnNumofByte[nTmp1-1]++;
	}

	nNumofWord = GetPrivateProfileInt(_T("Number"),_T("Word"),0,chConfigFile);
	if(nNumofWord>31 && nNumofWord<0)
	{
//		AfxMessageBox(IDS_ERRORCONFIGFILE);
		return FALSE;
	}
	
	for(i=0;i<nNumofWord;i++)
	{
		_stprintf(chTmp1,_T("Word%i"),i);
		nTmp1 = GetPrivateProfileInt(chTmp1,_T("Page"),10,chConfigFile);
		if(nTmp1>nTotalPage && nTmp1<1)
		{
//			AfxMessageBox(IDS_ERRORCONFIGFILE);
			return FALSE;
		}
		pnNumofWord[nTmp1-1]++;
	}

	nNumofBit = GetPrivateProfileInt(_T("Number"),_T("Bit"),0,chConfigFile);
	if(nNumofBit>200 && nNumofBit<0)
	{
//		AfxMessageBox(IDS_ERRORCONFIGFILE);
		return FALSE;
	}
	
	for(i=0;i<nNumofBit;i++)
	{
		_stprintf(chTmp1,_T("Bit%i"),i);
		nTmp1 = GetPrivateProfileInt(chTmp1,_T("Page"),10,chConfigFile);
		if(nTmp1>nTotalPage && nTmp1<1)
		{
//			AfxMessageBox(IDS_ERRORCONFIGFILE);
			return FALSE;
		}
		pnNumofBit[nTmp1-1]++;
	}

	nNumofMultiBit = GetPrivateProfileInt(_T("Number"),_T("MultiBit"),30,chConfigFile);
	if(nNumofMultiBit>200 && nNumofMultiBit<0)
	{
//		AfxMessageBox(IDS_ERRORCONFIGFILE);
		return FALSE;
	}
	
	for(i=0;i<nNumofMultiBit;i++)
	{
		_stprintf(chTmp1,_T("MultiBit%i"),i);
		nTmp1 = GetPrivateProfileInt(chTmp1,_T("Page"),10,chConfigFile);
		if(nTmp1>nTotalPage && nTmp1<1)
		{
//			AfxMessageBox(IDS_ERRORCONFIGFILE);
			return FALSE;
		}
		pnNumofMultiBit[nTmp1-1]++;
	}

	for(j=0;j<nTotalPage;j++)
	{
		pByte=new ConfigByte[pnNumofByte[j]];
		for(i=0;i<pnNumofByte[j];i++)
		{
			pByte[i].pEdit=NULL;
			pByte[i].pStatic=NULL;
		}

		pWord=new ConfigWord[pnNumofWord[j]];
		for(i=0;i<pnNumofWord[j];i++)
		{
			pWord[i].pEdit=NULL;
			pWord[i].pStatic=NULL;
		}

		pBit=new ConfigBit;
		pBit->pGroupBox=NULL;
		pMultiBit=new ConfigMultiBit[pnNumofMultiBit[j]];
		int nCount=0;
		for(i=0;i<nNumofByte;i++)
		{
			_stprintf(chTmp1,_T("Byte%i"),i);
			nTmp1 = GetPrivateProfileInt(chTmp1,_T("Page"),10,chConfigFile);
			if((nTmp1-1)==j)
			{
				pByte[nCount].nID=BYTEID+i;
				GetPrivateProfileString(chTmp1,_T("Name"),_T(""),pByte[nCount].chName,24,chConfigFile);
				nTmp1=GetPrivateProfileInt(chTmp1,_T("Pos"),10,chConfigFile);
				pByte[nCount].nPos=nTmp1;
				nTmp1=GetPrivateProfileInt(chTmp1,_T("Low8Bit"),10,chConfigFile);
				pByte[nCount].bLow8Bit=nTmp1?TRUE:FALSE;
				nCount++;
			}
		}
		baseconfig[j].nNumofByte=pnNumofByte[j];
		baseconfig[j].pByte=pByte;
	
		nCount=0;
		for(i=0;i<nNumofWord;i++)
		{
			_stprintf(chTmp1,_T("Word%i"),i);
			nTmp1 = GetPrivateProfileInt(chTmp1,_T("Page"),10,chConfigFile);
			if((nTmp1-1)==j)
			{
				pWord[nCount].nID=WORDID+i;
				GetPrivateProfileString(chTmp1,_T("Name"),_T(""),pWord[nCount].chName,24,chConfigFile);
				nTmp1=GetPrivateProfileInt(chTmp1,_T("Pos"),10,chConfigFile);
				pWord[nCount].nPos=nTmp1;
				// Modified by QRJ, 2012.03.01
				nTmp1=GetPrivateProfileInt(chTmp1,_T("Decimal"),-1,chConfigFile);
				pWord[nCount].nDecimal=nTmp1;
				
				nCount++;
			}
		}
		baseconfig[j].nNumofWord=pnNumofWord[j];
		baseconfig[j].pWord=pWord;

		nCount=0;
		pBit->pSubBit=new ConfigSubBit[pnNumofBit[j]];
		_tcscpy(pBit->chName,_T("Option:"));
		for(i=0;i<pnNumofBit[j];i++)
		{
			pBit->pSubBit[i].pCheck=NULL;
		}

		TCHAR chTmp2[5];
		for(i=0;i<nNumofBit;i++)
		{
			_stprintf(chTmp1,_T("Bit%i"),i);
			nTmp1 = GetPrivateProfileInt(chTmp1,_T("Page"),10,chConfigFile);
			if((nTmp1-1)==j)
			{
				pBit->pSubBit[nCount].nID=BITID+i;
				GetPrivateProfileString(chTmp1,_T("Name"),_T(""),pBit->pSubBit[nCount].chName,29,chConfigFile);
				nTmp1=GetPrivateProfileInt(chTmp1,_T("Pos"),10,chConfigFile);
				pBit->pSubBit[nCount].nPos=nTmp1;
				nTmp1=GetPrivateProfileInt(chTmp1,_T("EnOperation"),10,chConfigFile);
				pBit->pSubBit[nCount].bEnable=nTmp1?TRUE:FALSE;
				GetPrivateProfileString(chTmp1,_T("Enable"),_T(""),chTmp2,5,chConfigFile);
				pBit->pSubBit[nCount].nEnable=_tcstol(chTmp2,NULL,16);
				GetPrivateProfileString(chTmp1,_T("Disable"),_T(""),chTmp2,5,chConfigFile);
				pBit->pSubBit[nCount].nDisable=_tcstol(chTmp2,NULL,16);
				nCount++;
			}
		}
		baseconfig[j].nNumofBit=pnNumofBit[j];
		baseconfig[j].pBit=pBit;

		nCount=0;
		int nIDADD=0;
		for(k=0;k<nNumofMultiBit;k++)
		{
			_stprintf(chTmp1,_T("MultiBit%i"),k);
			nTmp1 = GetPrivateProfileInt(chTmp1,_T("Page"),10,chConfigFile);
			if((nTmp1-1)==j)
			{
				pMultiBit[nCount].nFirstID=MULTIBITID+nIDADD;
				GetPrivateProfileString(chTmp1,_T("Name"),_T(""),pMultiBit[nCount].chName,MAXDIRECTNAME,chConfigFile);
				pMultiBit[nCount].nPos=GetPrivateProfileInt(chTmp1,_T("Pos"),10,chConfigFile);;
				GetPrivateProfileString(chTmp1,_T("Program"),_T(""),chTmp2,5,chConfigFile);
				pMultiBit[nCount].nProgram=_tcstol(chTmp2,NULL,16);
				nTmp1 = GetPrivateProfileInt(chTmp1,_T("Number"),10,chConfigFile);
				pMultiBit[nCount].nNumofSmall=nTmp1;
				pMultiBit[nCount].pSmall=new ConfigSmallMultiBit[nTmp1];
				pMultiBit[nCount].pGroupBox=NULL;
				pMultiBit[nCount].pComboBox=NULL;
				pMultiBit[nCount].pStatic=NULL;
				for(i=0;i<pMultiBit[nCount].nNumofSmall;i++)
				{
					pMultiBit[nCount].pSmall[i].pRadio=NULL;
				}
				TCHAR chTmp3[10];
				for(i=0;i<pMultiBit[nCount].nNumofSmall;i++)
				{
					pMultiBit[nCount].pSmall[i].nID=pMultiBit[nCount].nFirstID+i;
					_stprintf(chTmp3,_T("SubName%i"),i);
					GetPrivateProfileString(chTmp1,chTmp3,_T(""),pMultiBit[nCount].pSmall[i].chName,MAXDIRECTNAME,chConfigFile);
					_stprintf(chTmp3,_T("Value%i"),i);
					GetPrivateProfileString(chTmp1,chTmp3,_T(""),chTmp2,5,chConfigFile);
					pMultiBit[nCount].pSmall[i].nValue=_tcstol(chTmp2,NULL,16);
				}
				nIDADD+=pMultiBit[nCount].nNumofSmall;
				nCount++;
			}
		}
		baseconfig[j].nNumofMultiBit=pnNumofMultiBit[j];
		baseconfig[j].pMultiBit=pMultiBit;

		baseconfig[j].nConfigType=nConfigType;
	//	dlgPropertySheet.AddPage(&baseconfig[j]);	
	}

	


// #ifndef AGSOFT
// 	_tunlink(TMPFILE);
// 	dlgPropertySheet.m_psh.dwFlags|=PSH_NOAPPLYNOW;
// #endif
// 	PRJ& prj=glPrjList[sysValue.curModule];
// 	memcpy(nConfigWord,prj.par.configWord,sizeof(nConfigWord));
// 	if(dlgPropertySheet.DoModal()==IDOK)
// 	{
// 		memcpy(prj.par.configWord,nConfigWord,sizeof(nConfigWord));
// 		STRING_MESSAGE strMsg={_T(""), 0};
// 		GetLanguage("/Language/LogInfo/ConfigModified",strMsg.message);
// 		_CallBackMsgInfoFunc(sysValue.curModule,int(&strMsg));
// 	}

	delete chTmp;
	delete [] baseconfig;
	delete pnNumofByte;
	delete pnNumofWord;
	delete pnNumofBit;
	delete pnNumofMultiBit;
	return TRUE;
}
void CBaseConfig::Apply(void)
{
	if(bInitPage==FALSE)
		return;
	int j,k;
	TCHAR chTmp[10];
	int nTmp;

	if(nNumofByte)
	{
		for(k=0;k<nNumofByte;k++)
		{
			pByte[k].pEdit->GetWindowText(chTmp,3);
			nTmp=_tcstol(chTmp,NULL,16);
			if(pByte[k].bLow8Bit)
			{
				nConfigWord[pByte[k].nPos] &=0xff00;
				nConfigWord[pByte[k].nPos] |= (nTmp&0xff);
			}
			else
			{
				nConfigWord[pByte[k].nPos] &=0xff;
				nConfigWord[pByte[k].nPos] |= ((nTmp<<8)&0xff00);
			}
		}
	}

	if(nNumofWord)
	{
		for(k=0;k<nNumofWord;k++)
		{
			pWord[k].pEdit->GetWindowText(chTmp,10);
			//Modified by QRJ, 2012.03.01
			if(pWord[k].nDecimal==-1)
				nTmp=_tcstol(chTmp,NULL,16);
			else{
				nTmp=_tcstod(chTmp,NULL)*pow((double)10,(double)pWord[k].nDecimal);
			}
			nConfigWord[pWord[k].nPos]=nTmp;
		}
	}

	BOOL bStatus;
	int nTmp1,nTmp2;
	if(nNumofBit)
	{
		for(k=0;k<nNumofBit;k++)
		{
			bStatus=(pBit->pSubBit[k].pCheck)->GetCheck();
			if(pBit->pSubBit[k].bEnable)
			{
				nTmp1=pBit->pSubBit[k].nDisable;
				nTmp2=pBit->pSubBit[k].nEnable;
				nConfigWord[pBit->pSubBit[k].nPos] &= nTmp1;
				if(bStatus)
					nConfigWord[pBit->pSubBit[k].nPos] |= nTmp2;
			}
			else
			{
				nTmp2=pBit->pSubBit[k].nDisable;
				nTmp1=pBit->pSubBit[k].nEnable;
				nConfigWord[pBit->pSubBit[k].nPos] &= nTmp1;
				if(!bStatus)
					nConfigWord[pBit->pSubBit[k].nPos] |= nTmp2;
			}
		}
	}

	if(nNumofMultiBit)
	{
		for(j=0;j<nNumofMultiBit;j++)
		{
			if(nConfigType == 10)
			{
				pMultiBit[j].nWhichSelect=pMultiBit[j].pComboBox->GetCurSel();
			}

			if(pMultiBit[j].nWhichSelect < pMultiBit[j].nNumofSmall)
			{
				nConfigWord[pMultiBit[j].nPos] &= pMultiBit[j].nProgram;
				nTmp= pMultiBit[j].pSmall[pMultiBit[j].nWhichSelect].nValue;
				nConfigWord[pMultiBit[j].nPos] |= nTmp;
			}
		}
	}
}

BOOL CBaseConfig::OnKillActive()
{
	// TODO: Add your specialized code here and/or call the base class
	Apply();
	return CPropertyPage::OnKillActive();
}

void CBaseConfig::OnShowWindow(BOOL bShow, UINT nStatus)
{
	CPropertyPage::OnShowWindow(bShow, nStatus);

	// TODO: Add your message handler code here
/*	if(bInitPage==TRUE){
		CRect rect;
		GetWindowRect(rect);
		SetWindowPos(NULL,0,0,rect.Width(),300,SWP_NOMOVE|SWP_NOZORDER);
		
	}*/
}
